package com.team.app.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

public class UpdateFileUtil {
	private static final Logger logger = LoggerFactory.getLogger(UpdateFileUtil.class);
	
	
	/* Convert image File to Base64
	 * Using display <img id="imageShow" src="data:image/png;base64,${obj.image}" alt="IMG DESC" width="150px" height="200px">
	 * With uploadImage.jsp
	 */
	public static String getContentImage(HttpServletRequest req){
		try {
			MultipartHttpServletRequest multipartReq = (MultipartHttpServletRequest)req;
			MultipartFile multipartFile = multipartReq.getFile("myFile");	
			BufferedImage image = ImageIO.read(multipartFile.getInputStream());			
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
		    ImageIO.write(image, "png", baos);
		    byte[] res=baos.toByteArray();
		    String encodedImage = Base64.encode(res);
		    logger.info(encodedImage);
		    return encodedImage;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	
	
	
	
	/*
	public static String getContentImage(HttpSession s,String urlFixed){	
		try {
			MultipartFile multipartFile = getUploadImage(s,urlFixed);
			BufferedImage image = ImageIO.read(multipartFile.getInputStream());			
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
		    ImageIO.write(image, "png", baos);
		    byte[] res=baos.toByteArray();
		    String encodedImage = Base64.encode(res);
		    return encodedImage;
		} catch (Exception e) {			
		}
		return null;
	}
	
	public static MultipartFile getUploadImage(HttpSession s,String urlFixed){	
		urlFixed = urlFixed.replace("/", "_");
		urlFixed = "f"+urlFixed;
		return (MultipartFile)s.getAttribute(urlFixed);
	}	
	
	public static void uploadImage(HttpSession s,HttpServletRequest req,HttpServletResponse res){
		String urlFixed = "f_"+req.getParameter("urlFixed");
		MultipartHttpServletRequest multipartReq = null;
		BufferedImage bufferImage = null;
		try {
			multipartReq = (MultipartHttpServletRequest)req;
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {			
			if(multipartReq != null){				
				MultipartFile multipartFile = multipartReq.getFile("myFile");	
				s.setAttribute(urlFixed, multipartFile);				
			}
			MultipartFile multipartFile = (MultipartFile)s.getAttribute(urlFixed);
			
			if(multipartFile != null){
				bufferImage = ImageIO.read(multipartFile.getInputStream());			
				res.setContentType("image/png");			
				OutputStream out = res.getOutputStream();
				ImageIO.write(bufferImage, "jpg", out);			
				out.close();
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	*/
}
